﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AntUnion.FrameWork.Model.Models;
using Util.Extension;
using Microsoft.AspNetCore.Hosting;
using AntUnion.FrameWork.Util;
using Microsoft.AspNetCore.Mvc.Rendering;
using AntUnion.FrameWork.Web.Extend;

namespace AntUnion.FrameWork.Web.Areas.Console.Controllers
{
    [Area("Console")]
    public class CommissionDetailedReportController : BaseController
    {
        private readonly mtContext _context;

        public CommissionDetailedReportController(mtContext context, IWebHostEnvironment env) : base(context, env)
        {
            _context = context;
        }

        #region 返佣明细列表
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取账户列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetListJson(int page, int limit, int? accountGroup = null, long? tradingAccountNo = null, string symbol = "", string fullName = "", string registrationTime = "")
        {
            ListResult res = new ListResult();
            try
            {
                var data = from y in _context.CommissionDetailedReports
                           join o in _context.CrmUsers on y.Owner equals o.Id
                           join c in _context.CrmUsers on y.Crmid equals c.Id
                           select new
                           {
                               id = y.Id,
                               owner = y.Owner,
                               ownerFullName = o.Name + "/" + o.LoginName,
                               crmId = y.Crmid,
                               tradingAccount = y.Mt5login,
                               traders = c.Name + "/" + c.LoginName,
                               accountGroup = c.CrmGroup,
                               deal = y.Deal,
                               symbol = y.Symbol,
                               volume = y.Volume,
                               programme = y.Scheme, //_context.RebateSchemes.FirstOrDefault(k => k.Id == y.SchemeId).Names,
                               rebateLevel = y.Level, //_context.RebateLevels.FirstOrDefault(k => k.Id == y.LevelId).Names,
                               cardinal = y.Coefficient.ToString("#,##0.00"),
                               rebateAmount = y.Money.ToString("#,##0.00"),
                               rebateTime = y.Addtime
                           };

                if (accountGroup != null)
                {
                    data = data.Where(k => k.accountGroup == accountGroup);
                }
                if (!string.IsNullOrEmpty(symbol))
                {
                    data = data.Where(k => k.symbol.Contains(symbol));
                }
                if (!string.IsNullOrEmpty(fullName))
                {
                    data = data.Where(k => k.ownerFullName.Contains(fullName));
                }
                if (tradingAccountNo != null)
                {
                    data = data.Where(k => k.tradingAccount == tradingAccountNo);
                }
                if (!string.IsNullOrEmpty(registrationTime) && registrationTime.Contains(" - "))
                {
                    string[] registrationTimes = registrationTime.Split(new string[] { " - " }, StringSplitOptions.None);
                    DateTime startTime = Convert.ToDateTime(registrationTimes[0]);
                    DateTime endTime = Convert.ToDateTime(registrationTimes[1]);
                    data = data.Where(k => k.rebateTime > startTime && k.rebateTime < endTime);
                }
                if (page == 0 && limit == 0)
                {
                    res.Data = await data.OrderByDescending(k => k.rebateTime).ToArrayAsync();
                }
                else
                {
                    res.Data = await data.OrderByDescending(k => k.rebateTime).Skip((page - 1) * limit).Take(limit).ToArrayAsync();
                }
                res.Code = 0;
                res.Count = data.Count();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion

        #region 日返佣详情
        public IActionResult DayCommissionReport()
        {
            return View();
        }

        /// <summary>
        /// 获取账户列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetDayCommissionReportListJson(int page, int limit, string symbol = "", string fullName = "", string registrationTime = "", string balanceDate = "")
        {
            ListResult res = new ListResult();
            try
            {
                var data = from y in _context.DailyDetailedReports
                           join o in _context.CrmUsers on y.Owner equals o.Id
                           select new
                           {
                               id = y.Id,
                               owner = y.Owner,
                               ownerFullName = o.Name + "/" + o.LoginName,
                               symbol = y.Symbol,
                               volume = y.Volume,
                               programme = y.Scheme, //_context.RebateSchemes.FirstOrDefault(k => k.Id == y.SchemeId).Names,
                               rebateLevel = y.Level, //_context.RebateLevels.FirstOrDefault(k => k.Id == y.LevelId).Names,
                               cardinal = y.Coefficient.ToString("#,##0.00"),
                               rebateAmount = y.Money.ToString("#,##0.00"),
                               rebateTime = y.Addtime,
                               balanceDate = y.Date
                           };

                if (!string.IsNullOrEmpty(fullName))
                {
                    data = data.Where(k => k.ownerFullName.Contains(fullName));
                }
                if (!string.IsNullOrEmpty(symbol))
                {
                    data = data.Where(k => k.symbol.Contains(symbol));
                }
                if (!string.IsNullOrEmpty(registrationTime) && registrationTime.Contains(" - "))
                {
                    string[] registrationTimes = registrationTime.Split(new string[] { " - " }, StringSplitOptions.None);
                    DateTime startTime = Convert.ToDateTime(registrationTimes[0]);
                    DateTime endTime = Convert.ToDateTime(registrationTimes[1]);
                    data = data.Where(k => k.rebateTime > startTime && k.rebateTime < endTime);
                }
                if (!string.IsNullOrEmpty(balanceDate) && balanceDate.Contains(" - "))
                {
                    string[] balanceDates = registrationTime.Split(new string[] { " - " }, StringSplitOptions.None);
                    DateTime startTime = Convert.ToDateTime(balanceDates[0]);
                    DateTime endTime = Convert.ToDateTime(balanceDates[1]);
                    data = data.Where(k => k.balanceDate > startTime && k.balanceDate < endTime);
                }
                if (page == 0 && limit == 0)
                {
                    res.Data = await data.OrderByDescending(k => k.rebateTime).ToArrayAsync();
                }
                else
                {
                    res.Data = await data.OrderByDescending(k => k.rebateTime).Skip((page - 1) * limit).Take(limit).ToArrayAsync();
                }
                res.Code = 0;
                res.Count = data.Count();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion

        #region 日返佣统计
        public IActionResult DayRebateStat()
        {
            return View();
        }

        /// <summary>
        /// 获取账户列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetDayRebateStatListJson(int page, int limit, string fullName = "", string registrationTime = "", string balanceDate = "")
        {
            ListResult res = new ListResult();
            try
            {
                var data = from y in _context.DailyReports
                           join o in _context.CrmUsers on y.Owner equals o.Id
                           select new
                           {
                               id = y.Id,
                               owner = y.Owner,
                               ownerFullName = o.Name + "/" + o.LoginName,
                               volume = y.Count,
                               rebateAmount = y.Money.ToString("#,##0.00"),
                               rebateTime = y.Addtime,
                               balanceDate = y.Date
                           };

                if (!string.IsNullOrEmpty(fullName))
                {
                    data = data.Where(k => k.ownerFullName.Contains(fullName));
                }
                if (!string.IsNullOrEmpty(registrationTime) && registrationTime.Contains(" - "))
                {
                    string[] registrationTimes = registrationTime.Split(new string[] { " - " }, StringSplitOptions.None);
                    DateTime startTime = Convert.ToDateTime(registrationTimes[0]);
                    DateTime endTime = Convert.ToDateTime(registrationTimes[1]);
                    data = data.Where(k => k.rebateTime > startTime && k.rebateTime < endTime);
                }
                if (!string.IsNullOrEmpty(balanceDate) && balanceDate.Contains(" - "))
                {
                    string[] balanceDates = registrationTime.Split(new string[] { " - " }, StringSplitOptions.None);
                    DateTime startTime = Convert.ToDateTime(balanceDates[0]);
                    DateTime endTime = Convert.ToDateTime(balanceDates[1]);
                    data = data.Where(k => k.balanceDate > startTime && k.balanceDate < endTime);
                }
                if (page == 0 && limit == 0)
                {
                    res.Data = await data.OrderByDescending(k => k.rebateTime).ToArrayAsync();
                }
                else
                {
                    res.Data = await data.OrderByDescending(k => k.rebateTime).Skip((page - 1) * limit).Take(limit).ToArrayAsync();
                }
                res.Code = 0;
                res.Count = data.Count();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion

        #region 用户返佣统计 
        public IActionResult UserRebateStat()
        {
            return View();
        }

        /// <summary>
        /// 获取账户列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetUserRebateStatListJson(int page, int limit, int? accountGroup = null, long? tradingAccountNo = null, string fullName = "", string registrationTime = "", string balanceDate = "")
        {
            ListResult res = new ListResult();
            try
            {
                var data = from y in _context.PersonReports
                           join o in _context.CrmUsers on y.Owner equals o.Id
                           join c in _context.CrmUsers on y.Crmid equals c.Id
                           select new
                           {
                               id = y.Id,
                               owner = y.Owner,
                               ownerFullName = o.Name + "/" + o.LoginName,
                               crmId = y.Crmid,
                               tradingAccount = y.Mt5login,
                               traders = c.Name + "/" + c.LoginName,
                               accountGroup = c.CrmGroup,
                               programme = y.Scheme,// _context.RebateSchemes.FirstOrDefault(k => k.Id == y.SchemeId).Names,
                               rebateLevel = y.Level, //_context.RebateLevels.FirstOrDefault(k => k.Id == y.LevelId).Names,
                               cardinal = y.Coefficient.ToString("#,##0.00"),
                               rebateAmount = y.Money.ToString("#,##0.00"),
                               rebateTime = y.Addtime,
                               balanceDate = y.Date
                           };

                if (accountGroup != null)
                {
                    data = data.Where(k => k.accountGroup == accountGroup);
                }
                if (!string.IsNullOrEmpty(fullName))
                {
                    data = data.Where(k => k.ownerFullName.Contains(fullName));
                }
                if (tradingAccountNo != null)
                {
                    data = data.Where(k => k.tradingAccount == tradingAccountNo);
                }
                if (!string.IsNullOrEmpty(registrationTime) && registrationTime.Contains(" - "))
                {
                    string[] registrationTimes = registrationTime.Split(new string[] { " - " }, StringSplitOptions.None);
                    DateTime startTime = Convert.ToDateTime(registrationTimes[0]);
                    DateTime endTime = Convert.ToDateTime(registrationTimes[1]);
                    data = data.Where(k => k.rebateTime > startTime && k.rebateTime < endTime);
                }
                if (!string.IsNullOrEmpty(balanceDate) && balanceDate.Contains(" - "))
                {
                    string[] balanceDates = registrationTime.Split(new string[] { " - " }, StringSplitOptions.None);
                    DateTime startTime = Convert.ToDateTime(balanceDates[0]);
                    DateTime endTime = Convert.ToDateTime(balanceDates[1]);
                    data = data.Where(k => k.balanceDate > startTime && k.balanceDate < endTime);
                }
                if (page == 0 && limit == 0)
                {
                    res.Data = await data.OrderByDescending(k => k.rebateTime).ToArrayAsync();
                }
                else
                {
                    res.Data = await data.OrderByDescending(k => k.rebateTime).Skip((page - 1) * limit).Take(limit).ToArrayAsync();
                }
                res.Code = 0;
                res.Count = data.Count();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion
    }
}
